#include "sqlitelibrary.h"
#include <stdio.h>

#pragma comment(lib, "SQLiteLibrary.lib")

using namespace LIBSQLITE4CONFIGSPEACE;

int ConfigExample()
{
	printf("LiteSQLite4Config Example\n");

	SQLite4Config*handle = SQLite4Config::GetConfig("TestConfig");
	printf("Handle:%p\n", handle);

	long id = handle->Insert("Name", "Li");
	printf("Insert ID:%d\n", id);

	std::string val = handle->Find("Name");
	printf("Find Result:%s\n", val.c_str());

	id = handle->update("Name", "HAHAHAHAHHA");
	printf("Update ID:%d\n", id);

	val = handle->Find("Name");
	printf("Find Result:%s\n", val.c_str());


	handle->Remove("Name");
	val = handle->Find("Name");
	printf("Find Result:%s\n", val.c_str());
	return 0;
}

int MiniDBExample()
{
	printf("LiteDB Example\n");
	std::list<std::string> columns;
	columns.push_back("DeviceID");
	columns.push_back("StartTime");
	columns.push_back("EndTime");
	columns.push_back("FilePath");
	SQLite4MiniDB*handle = SQLite4MiniDB::GetMiniDB("TestMiniDB", columns);
	printf("Handle:%p\n", handle);

	std::list<std::string> values;
	values.push_back("12345646");
	values.push_back("12313213");
	values.push_back("0.0.0.0");
	values.push_back("F::::::::");
	
	int ret = handle->Insert(columns, values);
	if (ret < 0) {
		printf("MiniDB Insert Error:%d\n", ret);
	}
	else {
		printf("MiniDB Insert Success\n");
	}

	std::map<std::string, std::list<std::string>> results;
	columns.clear();
	results.clear();
	ret = handle->Find(columns, results, "");
	if (ret < 0) {
		printf("MiniDB Find Error:%d\n", ret);
	}
	else {
		printf("MiniDB Find Success\n");
	}

	columns.clear();
	results.clear();
	columns.push_back("EndTime");
	ret = handle->Find(columns, results, "");
	if (ret < 0) {
		printf("MiniDB Insert Error:%d\n", ret);
	}
	else {
		printf("MiniDB Insert Success\n");
	}

	columns.clear();
	values.clear();
	columns.push_back("DeviceID");
	columns.push_back("StartTime");
	columns.push_back("EndTime");
	columns.push_back("FilePath");
	values.push_back("af");
	values.push_back("123");
	values.push_back("afaf");
	values.push_back("DDDDD");
	ret = handle->Insert(columns, values);
	if (ret < 0) {
		printf("MiniDB Insert Error:%d\n", ret);
	}
	else {
		printf("MiniDB Insert Success\n");
	}

	columns.clear();
	values.clear();
	columns.push_back("FilePath");
	values.push_back("XXXXXX");
	ret = handle->Update(columns, values, "StartTime = 123");
	if (ret < 0) {
		printf("MiniDB Update Error:%d\n", ret);
	}
	else {
		printf("MiniDB Update Success\n");
	}

	columns.clear();
	results.clear();
	ret = handle->Find(columns, results, "");
	if (ret < 0) {
		printf("MiniDB Find Error:%d\n", ret);
	}
	else {
		printf("MiniDB Find Success\n");
	}
	columns.push_back("FilePath");
	results.clear();
	ret = handle->Find(columns, results, "StartTime = 123");
	if (ret < 0) {
		printf("MiniDB Find Error:%d\n", ret);
	}
	else {
		printf("MiniDB Find Success\n");
	}
	return 0;
}

int main(int argc, char*argv[])
{
	printf("SQLiteLibrary Example\n");
	ConfigExample();
	MiniDBExample();

	printf("Finished!\n");

	getchar();
	return 0;
}